package de.tudarmstadt.ukp.muip.fibqdojo.visitor;

import java.util.List;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;

import de.tudarmstadt.ukp.muip.fibqdojo.Sentence;
import de.tudarmstadt.ukp.muip.fibqdojo.SentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.Token;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IStructureVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.japanese.JapaneseToken;

public class CopySentenceVisitor implements IStructureVisitor{
    
    private Optional<IFIBQSentence> copy = Optional.absent();
    
    public Optional<IFIBQSentence> getCopy() {
        return copy;
    }
    
    @Override
    public void visit(JapaneseToken token) {
    }

    @Override
    public void visit(Token token) {
    }

    @Override
    public void visit(SentenceToken token) {
    }

    @Override
    public void visit(Sentence sentence) {
        List<IFIBQSentenceToken> tokenCopies = copySentenceTokens(sentence);
        Sentence theCopy = new Sentence(tokenCopies.toArray(new SentenceToken[0]));
        setMarking(sentence, theCopy);
        IFIBQSentence wrap = (IFIBQSentence) theCopy;
        copy = Optional.of(wrap);
    }

    private void setMarking(Sentence sentence, Sentence theCopy) {
        boolean marked = sentence.isMarked();
       theCopy.setMarked(marked);
    }

    private List<IFIBQSentenceToken> copySentenceTokens(Sentence sentence) {
        List<IFIBQSentenceToken> tokenCopies = Lists.newArrayList();
        for (IFIBQSentenceToken token : sentence.getSentenceTokens()){
            CopySentenceTokenVisitor visitor = new CopySentenceTokenVisitor();
            token.accept(visitor);
            Optional<IFIBQSentenceToken> copy = visitor.getCopy();
            if(copy.isPresent()){
                tokenCopies.add(copy.get());
            }
        }
        return tokenCopies;
    }

}
